<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Database Chunkd Monitor</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <style>
        body.IE_M7, body.IE_M8, body.Win6{font-family:"Segoe UI", Tahoma, Verdana, Arial;}
        body.Safari{min-width:987px;}
        body.SF_iPhone{-webkit-text-size-adjust:none;}
        body
        {
            margin:0;font-family:Tahoma, Verdana, Arial;
            background-color:#FEFEFE;
            color:#333;
            font-size:12pt;
            width:100%;
            height:100%;
        }
        form{margin:0px;display: inline}
        #mainframe{position:relative;background-color:#FEFEFE;width:100%;height:100%;}
        #disk{position:absolute;background-color:#F4FBFF;display:inline;
            border:1px solid #81BEF7;padding:0px;left:0px;top:0px;width:100%;height:100%;}
        .disklist{position:relative;padding:0px; background-color:#D0E9F7;width:100%}
        .disktitle{position:relative;background-color:#2F5FA1;color:#c0c0c0; height:30px;}
        .viewtitle{position:relative;background-color:#2F5FA1;color:#c0c0c0; height:30px;}
        .infoview{position:relative;padding:0px; background-color:#D0E9F7;width:100%}
        #disksview{position:relative;background-color:#D0E9F7;color:#2F5FA1;width:100%;}
        .disk-title{background-color:#2F5FA1;color:#c0c0c0;height:30px;width:100%;}
        .ultab{list-style:none;padding:0;margin:0;width:100%;}
        .ltabname{cursor:pointer;font-size:12pt;}
        .ltab{background-color:#D0E9F7;cursor: pointer;width:100%;}
        .ltab-selected{background-color:#F4FBFF;cursor: pointer;width:100%;}
        .lfname{cursor: pointer;}
        .ldisk{background-color:#F4FBFF;cursor: pointer;}
        .ldisk-selected{background-color:#D0E9F7;cursor: pointer;}
        #diskform{position:absolute;width:500px;height:50px;background-color:#ffffff;
            border:1px solid #81BEF7;padding-top:10px;padding-left:6px;font-size:12pt;
            color:#2F5FA1;z-index:99;overflow:auto;display:none;}
        .eadd{color:#5FB404;font-size:13pt;font-weight:bolder;cursor:pointer;}
        .eupdate{color:#FF8000;font-size:13pt;font-weight:bolder;cursor:pointer;}
        .edelete{color:#B4045F;font-size:13pt;font-weight:bolder;cursor:pointer;}
        .eview{color:#0000ff;font-size:13pt;font-weight:bolder;cursor:pointer;}
        .closetxt{cursor:pointer;font-size:10pt;color:#cccccc;text-align:right;}
        .ndisk{display:inline;color:red;font-size:12pt;}
        </style>
        <script language='javascript'>
            var oTimer = null;
            var chradd = '✚', chrfold = '►', chrtree = '▼', chredit='✎', chrdel='✘', chrview = '☜ ';
            var base64EncodeChars = 
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
            var base64DecodeChars = new Array(
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
            52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
            -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
            15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
            -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
            41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1);
            function base64encode(str) 
            {
                var out, i, len;
                var c1, c2, c3;

                len = str.length;
                i = 0;
                out = "";
                while(i < len) {
                    c1 = str.charCodeAt(i++) & 0xff;
                    if(i == len)
                    {
                        out += base64EncodeChars.charAt(c1 >> 2);
                        out += base64EncodeChars.charAt((c1 & 0x3) << 4);
                        out += "==";
                        break;
                    }
                    c2 = str.charCodeAt(i++);
                    if(i == len)
                    {
                        out += base64EncodeChars.charAt(c1 >> 2);
                        out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
                        out += base64EncodeChars.charAt((c2 & 0xF) << 2);
                        out += "=";
                        break;
                    }
                    c3 = str.charCodeAt(i++);
                    out += base64EncodeChars.charAt(c1 >> 2);
                    out += base64EncodeChars.charAt(((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4));
                    out += base64EncodeChars.charAt(((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6));
                    out += base64EncodeChars.charAt(c3 & 0x3F);
                }
                return out;
            }
            function base64decode(str)
            {
                var c1, c2, c3, c4;
                var i, len, out;

                len = str.length;
                i = 0;
                out = "";
                while(i < len) {
                    /* c1 */
                    do {
                        c1 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
                    } while(i < len && c1 == -1);
                    if(c1 == -1)
                    break;

                    /* c2 */
                    do {
                        c2 = base64DecodeChars[str.charCodeAt(i++) & 0xff];
                    } while(i < len && c2 == -1);
                    if(c2 == -1)
                    break;

                    out += String.fromCharCode((c1 << 2) | ((c2 & 0x30) >> 4));

                    /* c3 */
                    do {
                        c3 = str.charCodeAt(i++) & 0xff;
                        if(c3 == 61)
                        return out;
                        c3 = base64DecodeChars[c3];
                    } while(i < len && c3 == -1);
                    if(c3 == -1)
                    break;

                    out += String.fromCharCode(((c2 & 0XF) << 4) | ((c3 & 0x3C) >> 2));

                    /* c4 */
                    do {
                        c4 = str.charCodeAt(i++) & 0xff;
                        if(c4 == 61)
                        return out;
                        c4 = base64DecodeChars[c4];
                    } while(i < len && c4 == -1);
                    if(c4 == -1)
                    break;
                    out += String.fromCharCode(((c3 & 0x03) << 6) | c4);
                }
                return out;
            }
            function utf16to8(str)
            {
                var out, i, len, c;
                out = "";
                len = str.length;
                for(i = 0; i < len; i++) {
                    c = str.charCodeAt(i);
                    if ((c >= 0x0001) && (c <= 0x007F)) {
                        out += str.charAt(i);
                        } else if (c > 0x07FF) {
                        out += String.fromCharCode(0xE0 | ((c >> 12) & 0x0F));
                        out += String.fromCharCode(0x80 | ((c >>  6) & 0x3F));
                        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
                        } else {
                        out += String.fromCharCode(0xC0 | ((c >>  6) & 0x1F));
                        out += String.fromCharCode(0x80 | ((c >>  0) & 0x3F));
                    }
                }
                return out;
            }
            function utf8to16(str)
            {
                var out, i, len, c;
                var char2, char3;
                out = "";
                len = str.length;
                i = 0;
                while(i < len) {
                    c = str.charCodeAt(i++);
                    switch(c >> 4)
                    { 
                        case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
                        // 0xxxxxxx
                        out += str.charAt(i-1);
                        break;
                        case 12: case 13:
                        // 110x xxxx   10xx xxxx
                        char2 = str.charCodeAt(i++);
                        out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
                        break;
                        case 14:
                        // 1110 xxxx  10xx xxxx  10xx xxxx
                        char2 = str.charCodeAt(i++);
                        char3 = str.charCodeAt(i++);
                        out += String.fromCharCode(((c & 0x0F) << 12) |
                        ((char2 & 0x3F) << 6) |
                        ((char3 & 0x3F) << 0));
                        break;
                    }
                }

                return out;
            }
            function start_wait()
            {
                document.body.style.cursor = 'wait';
            }
            function over_wait()
            {
                document.body.style.cursor = 'auto';
            }
            function base64strdecode(str)
            {
                return utf8to16(base64decode(str));
            }
            function trim(str)
            {
                return str.replace(/(^\s+|\s+$)/, "");
            }
            function removeElement(_element)
            {
                var _parentElement = _element.parentNode;
                if(_parentElement)
                {
                    _parentElement.removeChild(_element);  
                }
                return hideBFrame();
            }
            function onDiv(div, cssName)
            {
                div.className = cssName;
            }
            function outDiv(div, cssName)
            {
                div.className = cssName;
            }
            function CreateXMLHTTP()
            {
                var xmlhttp = null;
                xmlhttpObj = ["MSXML2.XmlHttp.5.0","MSXML2.XmlHttp.4.0",
                "MSXML2.XmlHttp.3.0","MSXML2.XmlHttp","Microsoft.XmlHttp"];
                if(window.XMLHttpRequest)
                {
                    xmlhttp = new XMLHttpRequest();
                }
                else if(window.ActiveXObject)
                {
                    for(i=0;i<xmlhttpObj.length;i++)    
                    {
                        try{xmlhttp = new ActiveXObject(xmlhttpObj[i]);break;}
                        catch(e){xmlhttp = null;}
                    }
                }
                else 
                {
                    alert('Can not create XMLHTTP Object');
                }
                return xmlhttp;
            }
            //close form
            function CloseThis(element)
            {
                element.parentNode.parentNode.style.display = "none";
            }
            function ClearDisks()
            {
                var disksview = document.getElementById('disksview');
                disksview.innerHTML = "";
                return true;
            }
            //list Disks 
            function ListDisks(txt)
            {
                var disksview = document.getElementById('disksview');
                var disks = null;
                var res = null;
                var html = "", css = "";
                if(txt && (res = eval(txt)) && disksview)
                {
                    disksview.innerHTML = "";
                    if((disks = res['disklist']))
                    {
                        //var osel = document.getElementById('disks');
                        var osel = document.forms['dataview']['disks'];
                        var j = 0;
                        for(j = osel.length - 1; j > 0; j--)
                        {
                            osel.remove(j);
                        }
                        var html = "<ul class='ultab'>";
                        j = 1;
                        for(x in disks)
                        {
                            var path = disks[x]['name'];
                            var port = disks[x]['sport'];
                            var limit = disks[x]['limit'];
                            var mode = disks[x]['mode'];
                            var max = disks[x]['max'];
                            var total = disks[x]['total'];
                            var max = disks[x]['max'];
                            var mask = disks[x]['mask'];
                            var s0 = "", s1 = "";
                            if(mode == 0) s0 = "selected";
                            if(mode == 1) s1 = "selected";
                            osel.options[j++] = new Option(path+":"+port, x);
                            html += "<li class='ltab' onmousemove=\"onDiv(this, 'ltab-selected')\"";
                            html += " onmouseout=\"outDiv(this, 'ltab')\" >";
                            html += "<form style='' action='javascript:void(0)' ";
                            html += " onsubmit='return DiskEdit(this);'>"
                            html += "<input type=hidden name='op' value='3'>";
                            html += "<input type=hidden name='diskid' value='"+x+"'>";
                            html += "编号:[<span class='ndisk'>"+x+"</span>]";
                            html += "&nbsp;&nbsp;";
                            html += "<select name='mode'>";
                            html += "<option value='0' "+s0+">磁盘存储</option>";
                            html += "<option value='1' "+s1+">mmap存储</option>";
                            html += "</select>";
                            html += "&nbsp;&nbsp;";
                            html += "数据量限制:<input type=text name='limit' value='"+limit+"' size=16>";
                            html += "&nbsp;&nbsp;";
                            html += "端口:[<span class='ndisk'>"+port+"</span>]";
                            html += "&nbsp;&nbsp;";
                            html += "磁盘:[<span class='ndisk'>"+path+"</span>]";
                            html += "&nbsp;&nbsp;";
                            html += "总数:[<span class='ndisk'>"+max+"</span>]";
                            html += "&nbsp;&nbsp;";
                            html += "组播:[<span class='ndisk'>"+mask+"</span>]";
                            html += "&nbsp;&nbsp;";
                            html += "<span class='edelete' title='Delete' ";
                            html += " onclick=\"DiskDel("+x+")\">"+chrdel+"</span>";
                            html += "</form></li>";
                        }
                        html += "</ul>";
                        disksview.innerHTML = html;
                    }
                    //alert(txt);
                }
            }
            //item view 
            function itemview(form)
            {
                var xmlHttp = null;
                var diskid = -1;
                var key = form['id'].value;
                var uri = form['url'].value;
                var o = null;
                var argv = "";
                if((o = form['disks'])) diskid = o.options[o.selectedIndex].value;
                var ox = document.getElementById('jsonview');
                if(diskid > 0 && (uri != "" || key != ""))
                {
                    ox.innerHTML = "";
                    argv = "op=5&diskid="+diskid;
                    if(key != "") argv += "&id="+key;
                    if(uri != "") argv += "&url="+base64encode(uri);
                    argv += "&rand="+Math.random();
                }
                if(argv != "" && (xmlHttp = CreateXMLHTTP()) != null)
                {
                    xmlHttp.open('POST', '/', true);
                    xmlHttp.setRequestHeader("Content-Length", argv.length);
                    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlHttp.onreadystatechange=function()
                    {
                        if(xmlHttp.readyState==4)
                        {
                            if(xmlHttp.status == 200)
                            {
                                var html = "<textarea cols=160 rows=30>"+xmlHttp.responseText+"</textarea>";
                                ox.innerHTML = html;
                            }
                        }
                    }
                    xmlHttp.send(argv);
                }
            }
            //list 
            function list()
            {
                var xmlHttp = null;
                if((xmlHttp = CreateXMLHTTP()) != null)
                {
                    var argv = "op=4&rand="+Math.random();
                    //alert(argv);
                    xmlHttp.open('POST', '/', true);
                    xmlHttp.setRequestHeader("Content-Length", argv.length);
                    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlHttp.onreadystatechange=function()
                    {
                        if(xmlHttp.readyState==4)
                        {
                            if(xmlHttp.status == 200)
                            {
                                ListDisks(xmlHttp.responseText);
                            }
                        }
                    }
                    xmlHttp.send(argv);
                }
            }
            //add
            function DiskEdit(form)
            {
                var xmlHttp = null;
                var path = "";
                var port = 0;
                var limit = 0;
                var diskid = -1;
                var o = null;
                var argv = "";
                if(form && (op = form['op'].value) > 0)
                {
                    if(op == 1 && (path = form['path'].value) != "" 
                        && (port = form['port'].value) > 0 
                        && (o = form['mode']) && (mode = o.options[o.selectedIndex].value) >= 0
                        && (limit = form['limit'].value) >= 0 
                        && (mask = form['mask'].value))
                    {
                        argv = "op=1&path="+path+"&port="+port+"&mode="+mode+"&limit="+limit
                        +"&mask="+mask;
                    }
                    else if(op == 2 && (diskid = form['diskid'].value) > 0)
                    {
                        argv = "op=2&diskid="+diskid;
                    }
                    else if(op == 3 && (diskid = form['diskid'].value) > 0 
                        && (limit = form['limit'].value) >= 0
                        && (o = form['mode']) && (mode = o.options[o.selectedIndex].value) >= 0)
                    {
                        argv = "op=3&diskid="+diskid+"&limit="+limit+"&mode="+mode;
                    }
                    ClearDisks();
                }else alert("请输入正确的数据");
                if(argv && (xmlHttp = CreateXMLHTTP()) != null)
                {
                    argv += "&rand="+Math.random();
                    xmlHttp.open('POST', '/', true);
                    xmlHttp.setRequestHeader("Content-Length", argv.length);
                    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlHttp.onreadystatechange=function()
                    {
                        if(xmlHttp.readyState==4)
                        {
                            if(xmlHttp.status == 200)
                            {
                                if(op == 1)
                                {
                                    form['path'].value = "";
                                    form['port'].value = "";
                                    form['limit'].value = 0;
                                    form['mask'].value = "235.8.8.0";
                                }
                                ListDisks(xmlHttp.responseText);
                            }
                        }
                    }
                    xmlHttp.send(argv);
                }
            }
            //del node
            function DiskDel(diskid)
            {
                
                var argv = "op=2&diskid="+diskid;
                if(argv && confirm("确认删除该任务?") && (xmlHttp = CreateXMLHTTP()) != null)
                {
                    ClearDisks();
                    //alert(argv);
                    xmlHttp.open('POST', '/', true);
                    xmlHttp.setRequestHeader("Content-Length", argv.length);
                    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                    xmlHttp.onreadystatechange=function()
                    {
                        if(xmlHttp.readyState==4)
                        {
                            if(xmlHttp.status == 200)
                            {
                                ListDisks(xmlHttp.responseText);
                            }
                        }
                    }
                    xmlHttp.send(argv);
                }
            }
            list();
        </script>
    </head>
    <!-- <body onload='javascript:loadInit();'> -->
        <!-- <body align=center onload="loadState()"> -->
    <body align=center >
    <div id='mainframe'>
    <div id='disk'>
        <div id=disk-header ></div>
            <table width=100% height=100% cellspacing=0 border=0 valign=top align=left >
                <tr>
                    <form name='dataview' style='' action='javascript:void(0)' onsubmit="return itemview(this);" >
                    <td  class='viewtitle' valign=top align=left >
                    <input type=hidden name='op' value='5'>
                    <select name='disks' ><option value=-1 >选择存储节点</option></select>
                    &nbsp;&nbsp;
                    ID:<input type=text name='id' size=32 >
                    或者URL:<input type=text name='url' size=96 >
                    &nbsp;&nbsp;
                    <input type=submit value='查询'>
                    <hr>
                    </td>
                    </form>
                </tr>

                <tr>
                    <form style='' action='javascript:void(0)' onsubmit="return DiskEdit(this);" >
                    <td class='disktitle' valign=top width=100%>
                        <input type=hidden name=op value=1>
                           <select name='mode' >
                            <option value='-1' >存储模式</option>
                            <option value='0' selected>磁盘存储</option>
                            <option value='1' >mmap存储</option>
                           </select>
                            路径:<input type=text name='path' value='' size=64 >
                            &nbsp;
                            端口:<input type=text name='port' value='' size=8 >
                            &nbsp;
                            数据量限制:<input type=text name='limit' value='0' size=16 >
                            &nbsp;
                            组播:<input type=text name='mask' value='235.8.8.0' size=16 >
                            &nbsp;
                            <input type=submit value='添加'>
                            <hr>
                    </td>
                    </form>
                </tr>
                <tr>
                    <td class='disklist' valign=top align=left >
                    <div id='disksview'></div>
                    <hr>
                    <div id='jsonview' ></div>
                    </td>
                </tr>
        </table>
    </div>
    </div>
    </div>
</body>
</html>
